
var Game = {
	
	id: 0,
	angle: 0,
	selection: null,
	intervalShakes: null,
	nbShakes: 0,
	x1: 0,
	x2: 0,
	y1: 0,
	y2: 0,
	z1: 0,
	z2: 0,
	sensitivity: 30,
	ended: false,
	duration: 0,
	drinkerId: null,
	
	init: function()
	{
		Game.phase1();
	},
	
	phase1: function()
	{
		console.log('game start soon');
		Game.displayPhase(1);
	},
	
	phase2: function()
	{
		console.log('countdown');
		Game.displayPhase(2);
	},
	
	phase3: function()
	{
		console.log('play');
		Game.displayPhase(3);
		
		$('.liquid').css({'bottom': '0%', 'height': '100%'});
		setTimeout(function(){ $('.liquid').css({'bottom': '0%', 'height': '10%'}); }, 500);
		setTimeout(function(){ $('.liquid').css({'bottom': '0%', 'height': '50%'}); }, 1500);
		
		$('.liquid-wrapper').click(function(aeEvent){
			aeEvent.preventDefault();
			Game.selectDrink(this.id);
			return false;
		});
		
		Game.start = (new Date).getTime();
	},
	
	phase4: function()
	{
		console.log('waiting');
		Game.displayPhase(4);
		
		$('#duration').html(Game.duration);
	},
	
	phase5: function()
	{
		console.log('results');
		Game.displayPhase(5);
	},
	
	onNewGameEvent: function()
	{
		Game.phase2();
	},
	
	onCountDownEvent: function(anSeconds)
	{		
		$('#counter').html(anSeconds);
	},
	
	onStartGameEvent: function(anGameId)
	{
		Game.id = anGameId;
		Game.phase3();
	},
	
	onEndGameEvent: function()
	{
		Game.phase5();
	},
	
	onResultEvent: function(asStatus, aaPlayers)
	{
		Game.phase5();
		
		$('#status').html(asStatus);
		console.log(aaPlayers);
	},
	
	displayPhase: function(anPhase)
	{
		
		$('.phase').hide();
		$('#phase'+anPhase).show();
	},
	
	selectDrink: function(asDrink)
	{
		if(!Game.selection)
		{
			Game.selection = asDrink;
			$('body').addClass('selection-made').addClass('selection-made-' + Game.selection);
			
			$('.liquid').css({'bottom': '-100%', 'height': '150%'});
			
			Game.startMeasuringShake();
		}
	},
	
	setSugarLevel: function(anLevel)
	{
		$('#sugar-level').css('height', anLevel);
	},
	
	startMeasuringShake: function()
	{
		Game.intervalShakes = setInterval(function () {
			var change = Math.abs(Game.x1 - Game.x2 + Game.y1 - Game.y2 + Game.z1 - Game.z2);
			
			if (change > Game.sensitivity) {
				Game.nbShakes++;
				Game.setSugarLevel(Game.nbShakes);
			}
			
			$('#nb-shakes').html(Game.nbShakes);
			
			if(Game.nbShakes >= 100)
			{
				clearInterval(Game.intervalShakes);
				Game.end();
				return;
			}
			
			// Update new position
			Game.x2 = Game.x1;
			Game.y2 = Game.y1;
			Game.z2 = Game.z1;
			
		}, 50);
	},
	
	end: function()
	{
		console.log('finished pooring sugar');
		
		/* Run a test. */
		Game.duration = (new Date).getTime() - Game.start;
		
		Game.ended = true;
		
		Game.phase4();
		
		$.ajax({
			url: '/play/doShake/' + Game.drinkerId + '/' + Game.id + '/' + Game.duration + '/' + Game.selection
		});
	},
	
	
	
	
	onDeviceOrientation: function(aeEvent)
	{	
		Game.angle = Math.round(aeEvent.gamma);
		//$('.liquid').css('webkitTransform', 'rotateZ(' + -Game.angle + 'deg)');
		
		alpha = Math.round(aeEvent.alpha);
		beta = Math.round(aeEvent.beta);
		gamma = Math.round(aeEvent.gamma); //Math.round(aeEvent.gamma);
		
		document.getElementById('alpha').innerHTML = alpha;
		document.getElementById('beta').innerHTML = beta;
		document.getElementById('gamma').innerHTML = gamma;
		
		// if(!Game.selection)
		// 		{
		// 			if(Game.angle <= -70)
		// 			{
		// 				Game.selectDrink('tea');
		// 			}
		// 			else if(Game.angle >= 70)
		// 			{
		// 				Game.selectDrink('coffee');
		// 			}
		// 		}
	},

	onDeviceMotion: function(aeEvent)
	{
		if(!Game.ended)
		{
			Game.x1 = aeEvent.accelerationIncludingGravity.x;
			Game.y1 = aeEvent.accelerationIncludingGravity.y;
			Game.z1 = aeEvent.accelerationIncludingGravity.z;
		}
	}
	
};